﻿using System;
using System.IO.Ports;
using System.Windows;
using JDKD.BMCS.Instruction;
using NutShell.DesignPattern;
using System.Linq;

namespace JDKD.BMCS.InstructionTestApp
{
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
                public MainWindow()
                {
                        InitializeComponent();
                }

                #region 字段
                private String currentSerialPortName = String.Empty; 
                #endregion

                #region 属性


                public String CurrentSerialPortName
                {
                        get
                        {
                                return currentSerialPortName;
                        }
                        set
                        {
                                currentSerialPortName = value;
                                Singleton<SerialPortInstruction>.Instance.PortName = value;
                        }
                }

                public int CurrentCameraId { get; set; }

                public int CurrentChannelId { get; set; }

                #endregion 属性

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        var ports = SerialPort.GetPortNames();
                        foreach (var item in ports)
                        {
                                SerialPortComboBox.Items.Add(item);
                        }
                        if (ports.Count() > 0)
                        {
                                CurrentSerialPortName = ports.First();
                        }

                        for (int i = 1; i < 5; i++)
                        {
                                CameraComboBox.Items.Add(i);
                        }
                        CurrentCameraId = 1;

                        for (int i = 1; i < 9; i++)
                        {
                                ChannelComboBox.Items.Add(i);
                        }
                        CurrentChannelId = 1;

                        RootGrid.DataContext = this;
                }

                private void PushButton_Click(object sender, RoutedEventArgs e)
                {
                        if (PushButton.Content.ToString() == "推钢启动")
                        {
                                Singleton<SerialPortInstruction>.Instance.SendPushActionCommand(CurrentCameraId, true);
                                PushButton.Content = "推钢停止";
                                CameraComboBox.IsEnabled = false;
                        }
                        else
                        {
                                Singleton<SerialPortInstruction>.Instance.SendPushActionCommand(CurrentCameraId, false);
                                PushButton.Content = "推钢启动";
                                CameraComboBox.IsEnabled = true;
                        }
                }

                private void RaiseButton_Click(object sender, RoutedEventArgs e)
                {
                        if (RaiseButton.Content.ToString() == "挡板上升")
                        {
                                Singleton<SerialPortInstruction>.Instance.SendRaiseActionCommand(CurrentChannelId, true);
                                RaiseButton.Content = "挡板下降";
                                ChannelComboBox.IsEnabled = false;
                        }
                        else
                        {
                                Singleton<SerialPortInstruction>.Instance.SendRaiseActionCommand(CurrentChannelId, false);
                                RaiseButton.Content = "挡板上升";
                                CameraComboBox.IsEnabled = true;
                        }
                }

                private void ShiftButton_Click(object sender, RoutedEventArgs e)
                {

                }
        }
}